;layui.define(function (e) {
    layui.use(['index', 'role', 'jquery', 'http', 'common', 'layer', 'laytpl', 'treegrid'], function () {

        var $ = layui.jquery
            , http = layui.http
            , layer = parent.layer === undefined ? layui.layer : top.layer
            , laytpl = layui.laytpl
            , common = layui.common
            , treegrid = layui.treegrid;
        var url;
        treegrid.config.render = function (viewid, data) {
            var view = document.getElementById(viewid).innerHTML;
            return laytpl(view).render(data) || '';
        };

        var urls = {
            queryPageData: "/core/role/queryPageData",
            modifyDataPage: "/core/role/modifyDataDialog",
            modifyRoleMenuPage: "/core/role/menu/modifyRoleMenuDialog",
            modifyRoleMenuPermissionPage: "/core/role/menu/permission/modifyRoleMenuPermissionDialog",
            deleteData: "/core/role/deleteData"
        };
        queryPageData();
        var tableIns;

        var index;

        function queryPageData () {
            tableIns = treegrid.createNew({
                elem: 'demo',
                view: 'view',
                url: urls.queryPageData,
                id: 'id',
                parentid: 'pid',
                order: 'seq',
                onCollapse: true,
                searchData: {name: $('#name').val()},
                singleSelect: true
            });
            tableIns.build();
        }

        $("#btnAddPage").click(function () {
            url = urls.modifyDataPage;
            openDialog('添加角色', url);
        });

        fn.edit = function () {
            var row = tableIns.getRow();
            url = urls.modifyDataPage + "?id=" + row.id;
            openDialog('修改角色', url);
        };

        fn.del = function () {
            var row = tableIns.getRow();
            layer.confirm('删除该菜单将会删除菜单相关的数据,确定要删除吗？', {icon: 3, title: '提示信息'}, function (index) {
                http.post({
                    url: urls.deleteData + "/" + row.id, data: {}, success: function () {
                        common.showSuccessTips('数据删除成功',function () {
                            fn.refresh(true);
                        })
                    }
                });
            });
        };

        fn.addRoleMenu = function () {
            var row = tableIns.getRow();
            url = urls.modifyRoleMenuPage + "?roleId=" + row.id;
            openDialog('添加角色菜单', url);
        };

        fn.addRoleMenuPermission = function () {
            var row = tableIns.getRow();
            var roleId = row.pid.split('-')[0];
            url = urls.modifyRoleMenuPermissionPage + "?roleId=" + roleId + "&menuId=" + row.data.id + "&menuUrl=" + row.data.url;
            openDialog('添加角色菜单权限', url,'600px','500px');
        };

        fn.refresh = function (reload) {
            if (index)
                layui.layer.close(index);
            if (reload)
                queryPageData();
        };

        $("#btnQuery").on("click", function () {
            queryPageData();
        });

        var openDialog = function (title, url,width,height) {
            if(!width){
                width = '450px';
            }
            if(!height){
                height = '500px';
            }
            index = layui.layer.open({
                type: 2,
                title: title,
                shade : [0.8, '#C8E6C9'],
                maxmin: true,
                area: [width, height],
                content: url,
                btn: ['保存', '关闭'],
                btnAlign: 'c',
                //skin: 'layui-layer-admin',
                yes: function (index, layero) {
                    layero.find('iframe').contents().find("#formAdd").find("#btnAdd").click();
                },
                btn2: function (index) {
                    layui.layer.close(index);
                }
            });
        };

    });

    e("core/role", {})
});